package git.soulbgm.utils;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * servlet工具类
 *
 * @author SoulBGM
 * @date 2021/05/17
 */
public class ServletUtils {

    /**
     * get请求属性
     *
     * @return {@link ServletRequestAttributes}
     */
    public static ServletRequestAttributes getRequestAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }

    /**
     * 获取request
     */
    public static HttpServletRequest getRequest() {
        return getRequestAttributes().getRequest();
    }

    /**
     * 获取String参数
     */
    public static String getParameter(String name)
    {
        return getRequest().getParameter(name);
    }

    /**
     * 获取int参数
     */
    public static Integer getIntParameter(String name)
    {
        String parameter = getRequest().getParameter(name);
        if (parameter != null && !"".equals(parameter.trim())) {
            return Integer.parseInt(parameter);
        }
        return 0;
    }


}
